<template>
  <div class="permission">
    <el-card>
      <div class="btnRight">
        <el-button type="primary" @click="add(1, '0')">添加菜单 </el-button>
      </div>
      <el-table :data="list" row-key="id">
        <el-table-column label="菜单名称" prop="name"></el-table-column>
        <el-table-column label="权限标识" prop="code"></el-table-column>
        <el-table-column label="描述" prop="description"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <div>
              <el-button type="text" @click="add(2, row.id)"
                >添加权限点</el-button
              >
              <el-button type="text" @click="edit(row)">查看</el-button>
              <el-button type="text" @click="del(row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 弹框组件 -->
    <Dialog v-model="show" ref="dialog" @getData="getData"></Dialog>
  </div>
</template>

<script>
import { sysPermission, sysPermissionDel } from '@/api/permission.js'
import Dialog from './components/dialog.vue'
export default {
  components: { Dialog },
  data () {
    return {
      list: [],
      show: false
    }
  },
  created () {
    this.getData()
  },
  methods: {
    async getData () {
      const res = await sysPermission()
      this.list = this.changeData(res.data, '0')
      console.log('所有权限点', res)
      console.log('权限点树形结构', this.list)
    },
    changeData (arr, str = '0') {
      return arr.filter(item => {
        if (item.pid === str) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    // 删除
    async del (id) {
      this.$confirm('确定要删除吗', '提示')
        .then(async () => {
          await sysPermissionDel(id)
          this.$message.success('删除成功')
          this.getData()
        })
        .catch(() => {})
    },
    // 添加v菜单
    add (type, id) {
      this.show = true
      this.$refs.dialog.form.type = type
      this.$refs.dialog.form.pid = id
    },
    // 修改
    edit (row) {
      this.show = true
      this.$refs.dialog.mode = 'edit'
      this.$refs.dialog.form = JSON.parse(JSON.stringify(row))
    }
  }
}
</script>

<style lang="scss" scoped>
.permission {
  padding: 15px;
  .btnRight {
    text-align: right;
  }
}
</style>
